package zl.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.bean.Category;
import zl.bean.User;
import zl.mapper.NovelMapper;
import zl.mapper.UserMapper;
import zl.service.IndexService;
import zl.vo.option1Vo;
import zl.vo.option2Vo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NovelMapper novelMapper;
    @Override
    public option1Vo option1() throws SQLException {
        List<User> list = userMapper.selectAllUser(null);
        List<String> list1 = new ArrayList<>();
        List<Integer> list2 =new ArrayList<>();
        int i=1;
        for (User user : list) {
            System.out.println(user.toString());
            list1.add(user.getNickname());
            long count=novelMapper.selectNovelById(user.getId());
            list2.add((int)count);
        }
        option1Vo option1Vo = new option1Vo(list1, list2);
        return option1Vo;
    }

    @Override
    public List<option2Vo> option2() throws SQLException {
        List<Category> list = novelMapper.selectAllCategory(null);
        List<option2Vo> list1 = new ArrayList<>();
        for (Category category : list) {
            if(category.getParent_id()==0) {
                long count = novelMapper.selectChildren(category.getId());
                option2Vo option2Vo = new option2Vo((int)count, category.getName());
                list1.add(option2Vo);
            }else {
                long count = novelMapper.selectChildren(category.getId());
                if(list1.get(category.getParent_id()-1)!=null){
                option2Vo option2Vo=list1.get(category.getParent_id()-1);
                option2Vo.setValue(option2Vo.getValue()+(int)count);
                list1.set(category.getParent_id()-1,option2Vo);}
            }
        }
        return list1;
    }
}
